//
//  MainViewController.swift
//  MagicWeibo
//
//  Created by 李明禄 on 15/10/28.
//  Copyright © 2015年 http://blog.csdn.net/netluoriver. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    
    //在viewDidLoad中添加子控制器
    override func viewDidLoad() {
        
        tabBar.tintColor = UIColor.orangeColor()
       
        addChildViewControllers()
    }
    
 
     func addChildViewControllers() {
        addChildViewController(HomeViewController(),title: "首页", imageName: "tabbar_home")
        addChildViewController(DiscoverViewController(),title: "发现", imageName: "tabbar_discover")
        addChildViewController(UIViewController())
        addChildViewController(MessageViewController(),title: "消息", imageName: "tabbar_message_center")
        addChildViewController(ProfileViewController(),title: "我", imageName: "tabbar_profile")
    }
    
    
    func addChildViewController(controller: UITableViewController , title: String, imageName: String) {
        let vc = controller
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
    }
    
    
    //添加按钮
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        setupButtonFrame()
        
        //按钮添加监听
        composeButton.addTarget(self, action: "clickComposeButton", forControlEvents: UIControlEvents.TouchUpInside)
        
    }
    
    ///点击发表按钮的方法,这里不能用Private方法,因为添加的监听事件需要添加
    //TODO: 发表按钮的方法 未实现
    //warning:
    
     func clickComposeButton() {
        print(__FUNCTION__)
    }
    
    
    ///设置按钮的位置
    private func setupButtonFrame() {
    
        //这里不是用的tabbar.subViews.count
        //而是子控制器的childViewControllers.count
        let width = tabBar.bounds.width / CGFloat(childViewControllers.count)
        
        let rect = CGRectMake(0, 0, width, tabBar.bounds.height)
        
        //添加按钮的位置,理解CGRectOffset方法的用处
        composeButton.frame = CGRectOffset(rect, 2 * width, 0)
    }
    
    
    
    
    //懒加载button
    private lazy var composeButton: UIButton = {
        let button = UIButton()
        
        //设置背景颜色,减少工作量,此代码只是测试用
//        button.backgroundColor = UIColor.redColor()
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
       button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        //1.将按钮添加到tabBar中的妙用,理解为什么不将此代码添加到viewDidLayoutsubViews中,而是添加到这里,原因就是代码显示比较简洁
        //2.Swift 中的懒加载本质上是一个闭包，因此引用当前控制器的对象时需要使用 self.
        self.tabBar.addSubview(button)

        return button
        
    }()
    
}







